package com.tsing.common.result;


import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param <T>
 */

public class Result<T> implements Serializable {

    private Integer code; //编码：200成功，0和其它数字为失败
    private String errorMessage; //错误信息
    private T data; //数据

    // 添加 getter 和 setter 方法
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // 重写 toString 方法，使用 Gson 库将对象转换为 JSON 格式的字符串
    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = 200;
        return result;
    }
    
    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = 200;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.errorMessage = msg;
        result.code = 0;
        return result;
    }

}